package com.cn.kabalifemerchants.ui.login;


import com.cn.kabalifemerchants.api.Api;
import com.cn.kabalifemerchants.base.BasePresenter;
import com.cn.kabalifemerchants.callback.CacheCallBack;
import com.cn.kabalifemerchants.config.Constants;
import com.cn.kabalifemerchants.ui.login.bean.AgreementBean;
import com.cn.kabalifemerchants.ui.login.bean.LoginBean;
import com.cn.kabalifemerchants.ui.login.bean.LoginLibBean;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.login.bean.RegisterBean;
import com.cn.kabalifemerchants.ui.user.bean.MyUserInfoBean;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * Created by 晓林 on 2020/9/2.
 * function:
 */
public class LoginPresenter extends BasePresenter<LoginView> {

    /**
     * 验证码
     *
     * @param phone
     * @param type
     */
    public void onPhoneCodeData(String phone, int type) {
        OkGo.<MsgBean>post(Api.API_LOGIN_MSG)
                .params("phone", phone)
                .params("type", type)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("手机验证码：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onPhoneCodeSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 登录
     *
     * @param phone
     * @param pwd
     * @param type
     */
    public void onLoginData(String phone, String pwd, int type) {
        OkGo.<LoginBean>post(Api.API_LOGIN)
                .params("phone", phone)
                .params("pwd", pwd)
                .params("type", type)
                .execute(new CacheCallBack<LoginBean>(getView()._getContext(), true) {
                    @Override
                    public void onSuccess(Response<LoginBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("登录：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onLoginSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }


    /**
     * 注册
     *
     * @param phone
     * @param pwd
     * @param code
     * @param invite
     */
    public void onRegisterData(String phone, String pwd, String code, String invite) {
        OkGo.<RegisterBean>post(Api.API_LOGIN_REGISTER)
                .params("phone", phone)
                .params("pwd", pwd)
                .params("code", code)
                .params("invite", invite)
                .execute(new CacheCallBack<RegisterBean>() {
                    @Override
                    public void onSuccess(Response<RegisterBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("注册：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onRegisterSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    public void onUserInfoData(String token) {
        OkGo.<MyUserInfoBean>get(Api.API_USER_INFO)
                .headers("token", token)
                .execute(new CacheCallBack<MyUserInfoBean>() {
                    @Override
                    public void onSuccess(Response<MyUserInfoBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("我的" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUserInfoSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    public void onAgreementData(int type) {
        OkGo.<AgreementBean>get(Api.API_LOGIN_AGR)
                .params("type", type)
                .execute(new CacheCallBack<AgreementBean>() {
                    @Override
                    public void onSuccess(Response<AgreementBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("我的" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAgreementSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
}
